%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% NxN Principal Components
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [f,l,e,vnt] = pcn(x,k)

[t,n] = size(x);
x = x - repmat(mean(x),t,1);
vx = x*x'/t;
[ve, va, ve1] = svd(vx);

if k~=0; ve = ve(:,1:k);
         f = ve.*sqrt(n);
         l = x'*f/n;
         e = x-f*l';
         vnt = va(1:k,1:k);
else e = x; 
     f = zeros(t,1);
     l = zeros(n,1);
     vnt = 0;
end